//
//  XJDZViewController.m
//  YaDiuDiu
//
//  Created by 欧洪飞 on 2017/8/25.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "XJDZViewController.h"
#import "AddressPickerView.h"
@interface XJDZViewController ()<UIPickerViewDelegate,UIPickerViewDataSource,AddressPickerViewDelegate>{
    NSInteger _provinceIndex;   // 省份选择 记录
    NSInteger _cityIndex;       // 市选择 记录
    NSInteger _districtIndex;   // 区选择 记录
    NSMutableArray *array;
    NSString *_province;
    NSString *_city;
    NSString *_distric;
    
    UIView *bottomView;//底部view
}

@property (nonatomic,strong)UIPickerView *pickerView;
@property (weak, nonatomic) IBOutlet UILabel *labPlace;
@property (weak, nonatomic) IBOutlet UITextField *textPlace;
@property(nonatomic,strong)AddressPickerView *addressPickerView;
@end

@implementation XJDZViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    [self showNav:@"现居地址"];
//    _provinceIndex = _cityIndex = _districtIndex = 0;
//    NSString * path = [[NSBundle mainBundle] pathForResource:@"Province" ofType:@"plist"];
//
//    array = [[NSMutableArray alloc] initWithContentsOfFile:path];
    
    
    _labPlace.text = [NSString stringWithFormat:@"%@-%@-%@",_province,_city,_area];
    _textPlace.text = _det;
    _labPlace.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(loadUI)];
    [_labPlace addGestureRecognizer:tap];
    
}


- (void)loadUI{
    
    self.addressPickerView = [AddressPickerView showSelectCity];
    self.addressPickerView.delegate = self;
    //    _pickerView = [[UIPickerView alloc] initWithFrame:CGRectMake(0, ScreenHeight-216, ScreenWidth, 216)];
    //    _pickerView.backgroundColor = [UIColor whiteColor];
    //    _pickerView.dataSource = self;
    //    _pickerView.delegate = self;
    //    [self.view addSubview:_pickerView];
    //
    //    [self resetPickerSelectRow];
}

#pragma mark -- addressDelegate
-(void)sureBtnClickReturnProvince:(NSString *)province City:(NSString *)city Area:(NSString *)area
{
    _labPlace.text = [NSString stringWithFormat:@"%@-%@-%@",province,city,area];
    _province = province;
    _city = city;
    _distric = area;
}


-(void)resetPickerSelectRow
{
    [self.pickerView selectRow:_provinceIndex inComponent:0 animated:YES];
    [self.pickerView selectRow:_cityIndex inComponent:1 animated:YES];
    [self.pickerView selectRow:_districtIndex inComponent:2 animated:YES];
}

#pragma mark - PickerView Delegate

// 列数
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 3;
}

// 每列有多少行
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if(component == 0){
        return array.count;
    }
    else if (component == 1){
        return [array[_provinceIndex][@"citys"] count];
    }
    else{
        return [array[_provinceIndex][@"citys"][_cityIndex][@"districts"] count];
    }
}
// 返回每一行的内容
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if(component == 0){
        return array[row][@"province"];
    }
    else if (component == 1){
        return array[_provinceIndex][@"citys"][row][@"city"];
    }
    else{
        return array[_provinceIndex][@"citys"][_cityIndex][@"districts"][row];
    }
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
        pickerLabel.adjustsFontSizeToFitWidth = YES;
        [pickerLabel setTextAlignment:NSTextAlignmentCenter];
        [pickerLabel setBackgroundColor:[UIColor clearColor]];
        [pickerLabel setFont:[UIFont boldSystemFontOfSize:15]];
    }
    // Fill the label text here
    pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component];
    return pickerLabel;
}

// 滑动或点击选择，确认pickerView选中结果
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    
    if(component == 0){
        _provinceIndex = row;
        _cityIndex = 0;
        _districtIndex = 0;
        
        [self.pickerView reloadComponent:1];
        [self.pickerView reloadComponent:2];
    }
    else if (component == 1){
        _cityIndex = row;
        _districtIndex = 0;
        
        [self.pickerView reloadComponent:2];
    }
    else{
        _districtIndex = row;
    }
    
    // 重置当前选中项
    [self resetPickerSelectRow];
}
//添加现居地址
- (IBAction)clickTJ:(id)sender {
    if ([_labPlace.text isEqualToString:@"请选择省市区"]) {
        
        [LICGToast showWithText:@"请选择省市区"];
        return;
    }else if (_textPlace.text.length == 0){
        
        [LICGToast showWithText:@"请填写详细地址"];
        return;
    }
    
    [SVProgressHUD show];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    
    NSString *str = [NSString stringWithFormat:@"%@890d69e502d1d37a9e84335ccefece57",[user objectForKey:@"token2"]];
    NSString *token = [LCMD5Tool md5:str];
    NSMutableDictionary  *parmas = [NSMutableDictionary dictionary];
    [parmas setObject:token forKey:@"verifyToken"];
    [parmas setObject:[user objectForKey:@"userid"] forKey:@"userid"];
    [parmas setObject:_province forKey:@"province"];
    [parmas setObject:_city forKey:@"city"];
    [parmas setObject:_distric forKey:@"area"];
    [parmas setObject:_textPlace.text forKey:@"detailedAddress"];
    NSLog(@"parmas:%@",parmas);
    NSString *url = [NSString stringWithFormat:@"%@/App/Usercenter/addPresentAddress",urlHead];
    [[AFHTTPSessionManager manager] POST:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"res:%@",responseObject);
        NSInteger state = [[responseObject objectForKey:@"status"] integerValue];
        NSString *msg = [responseObject objectForKey:@"msg"];
        if (state == 200) {

            [self.navigationController popViewControllerAnimated:YES];

        }
        [SVProgressHUD showErrorWithStatus:msg];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
        [SVProgressHUD dismiss];
    }];
    
}


-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    // 省市区地址

    _province = array[_provinceIndex][@"province"];
    _city = array[_provinceIndex][@"citys"][_cityIndex][@"city"];
    _distric = array[_provinceIndex][@"citys"][_cityIndex][@"districts"][_districtIndex];
    
    NSString * address = [NSString stringWithFormat:@"%@-%@-%@", _province, _city, _distric];
    _labPlace.text = address;
    
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
    [bottomView removeFromSuperview];
//    province = array[_provinceIndex][@"province"];
//    city = array[_provinceIndex][@"citys"][_cityIndex][@"city"];
//    distric = array[_provinceIndex][@"citys"][_cityIndex][@"districts"][_districtIndex];
//    
//    NSString * address = [NSString stringWithFormat:@"%@-%@-%@", province, city, distric];
//    _labPlace.text = address;
//    
//    [[UIApplication sharedApplication].keyWindow endEditing:YES];
//    [bottomView removeFromSuperview];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
